package unwdmi.connector;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.mongodb.Cursor;
import com.mongodb.DBCursor;

import unwdmi.main.Main;
/**
 * The client connector handles request from clients
 * @author Stephan E G Veenstra
 *
 */
public class ClientConnector implements Runnable {

	private Socket clientSocket;
	
	private BufferedReader clientIN;
	private PrintWriter clientOUT;
	
	public ClientConnector(Socket clientSocket) {
		
		try {
			System.out.println("A Client has connected from: " + clientSocket.getInetAddress());
			this.clientSocket = clientSocket;
			clientIN = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			clientOUT = new PrintWriter(clientSocket.getOutputStream(), true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		try {
			String command;
			clientOUT.println("Welcome, you are connected to the Weather Server!");
			clientOUT.println("Please type your commands! [? for help]");
			while((command = clientIN.readLine()) != null){
				handleCommand(command);
				clientOUT.println("command: ");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Handles commands and turns them in to action
	 * @param command
	 * @throws IOException
	 */
	private void handleCommand(String command) throws IOException {
		String[] commands = command.split("\\s");
		if(commands[0].equals("close") || commands[0].equals("exit")){
			clientSocket.close();
			System.exit(-1);
		} else if(commands[0].equals("?")){
			clientOUT.println("type 'queries' for a selection of the queries or 'exit' or 'close' to close the connection");
		} else if(commands[0].equals("queries")) {
			this.clientOUT.println("Type [A] for a list with all the weather stations.");
			this.clientOUT.println("Type [B] for all measurements.");
			this.clientOUT.println("Type [C] for measurements of a specific station.");
			this.clientOUT.println("Type [D] for measurements of a specific country.");
		} else if(commands[0].equals("A")) {
			DBCursor cursor = Main.MONGO_INSTANCE.getCollection("stations").find();
			while(cursor.hasNext()) {
				this.clientOUT.println(cursor.next());		
			};
		} else if(commands[0].equals("B")) {
			DBCursor cursor = Main.MONGO_INSTANCE.getCollection("measurements").find();
			while(cursor.hasNext()) {
				this.clientOUT.println(cursor.next());		
			};
		} else if(commands[0].equals("C")) {
			if(commands.length > 1) {
				DBCursor cursor = Main.MONGO_INSTANCE.readStations("ID",commands[1]);
				while(cursor.hasNext()) {
					this.clientOUT.println(cursor.next());		
				};	
			} else {
				this.clientOUT.println("Please type ed. [C] 401650.");		
			};
		} else if(commands[0].equals("D")) {
			if(commands.length > 1) {
				DBCursor stations = Main.MONGO_INSTANCE.readStations("COUNTRY", commands[1]);
				while(stations.hasNext()) {
					DBCursor measurements = Main.MONGO_INSTANCE.readMeasurements("stn", stations.next().get("ID").toString());
					while(measurements.hasNext()) {
						this.clientOUT.println(measurements.next());
					};
				};	
			} else {
				this.clientOUT.println("Please type ed. [D] NORWAY.");		
			};
		};
		
		// TODO handle more requests?
	}

}
